package com.kkb.controller;
import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Player;
import com.kkb.service.PlayerService;
import com.kkb.vo.PlayerQueryVo;
import com.kkb.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@ResponseBody
@RequestMapping("/player")
public class PlayerController {
    @Autowired
    private PlayerService playerService;
    /**
     * 根据条件去查询
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultVo<Player> queryByPage(Integer pageNum, Integer pageSize, PlayerQueryVo vo){
        if (pageNum==null || pageNum<1){
            pageNum = 1;
        }
        if (pageSize==null || pageSize<1){
            pageSize=5;
        }
        PageInfo<Player> playerPageInfo = playerService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo<>(playerPageInfo);
    }

    /**
     * 添加一个球员
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVo<Player> addPlayer(Player player){
        int i = playerService.addPlayer(player);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器内部出现错误！");
    }

    /**
     * 通过 id查询球员信息
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResultVo<Player> queryById(@PathVariable("id")Integer playerId){
        Player player = playerService.queryById(playerId);
        return new ResultVo<>(player);
    }

    /**
     * 更新球员信息
     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Player> updatePlayer(@PathVariable("id") Integer playerId,Player player){
        player.setPlayerId(playerId);
        int i = playerService.updatePlayer(player);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"服务器内部出现错误，请稍后再试！");
    }
    /**
     * 删除一条球员信息
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVo<Player> deletePlayer(@PathVariable("id") Integer playerId){
        int i = playerService.deletePlayer(playerId);
        if (i==1){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"删除失败！服务器出现错误");
    }
}
